package com.ce.pms.system.custservice.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.system.custservice.dao.IEmergencyDegreeDao;
import com.ce.pms.system.custservice.model.EmergencyDegreeDO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 * 功能描述:  紧急程度维护服务Dao实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-18
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class EmergencyDegreeDaoImpl extends PmsBaseDaoImpl<EmergencyDegreeDO> implements
		IEmergencyDegreeDao {
	/**
	 * 方法描述: 分页查询所有紧急程度列表
	 * @author 胡煦
	 * Create on:2008-12-17
	 * @param searchVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryAllEmergencyDegreeByPage(SearchVO searchVO){
		StringBuffer queryStr =  new StringBuffer("FROM EmergencyDegreeDO ed where 1=1 ");
		if(!BlankUtil.isBlank(searchVO.getOrderKey())&&!BlankUtil.isBlank(searchVO.getOrderValue())){
			queryStr.append("order by ed.").append(searchVO.getOrderValue()).append(" ").
			append(searchVO.getOrderKey());
			return this.pagedQueryByPageNo(queryStr.toString(),searchVO.getPageNo(), searchVO.getPageSize());
		}
		return this.getEmergencyDegreeList(searchVO);

	}
	/**
	 * 方法描述: 紧急程度列表查询方法(根据序号升序排序，如果为空则在最后)
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param searchVO
	 * @return Page
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page getEmergencyDegreeList(SearchVO searchVO){
		String queryStr = "SELECT * FROM `CE_EMERGENCY_DEGREE` hgr ORDER BY IF(hgr.`SEQUENCE` is NULL,(SELECT max(hgr1.`SEQUENCE`) FROM `CE_EMERGENCY_DEGREE` hgr1)+1,hgr.`SEQUENCE`)";
		Page page = null;
		if(BlankUtil.isBlank(searchVO)){
			Query query = this.createSQLQuery(queryStr);
			page = new Page();
			page.setResult(this.changeObjToDo(query.list()));

		}else{
			page = this.pagedSqlQueryByPageNo(queryStr, searchVO.getPageNo(), searchVO.getPageSize());
			List<EmergencyDegreeDO> list = this.changeObjToDo(page.getResult());
			page.setResult(list);
		}
		return page;


	}
	/**
	 * 功能描述:根据紧急程度名字查询记录数<br>
	 * 注：新建判断name的唯一性时，oldId传为null；修改判断name的唯一性时，则需要传入本身的ID<br>
	 * @param name
	 * @param oldName
	 * @return
	 * Create author:胡煦
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public Long countEmergencyDegreeByName(String name,Long id){
		StringBuffer hql=new StringBuffer();
		List list = new ArrayList();
		list.add(name);

		hql.append("select count(*) FROM  EmergencyDegreeDO ed WHERE ed.name = ? ");
		if(id!=null){
			hql.append(" and ed.id <> ?");
			list.add(id);
		}
		Query query=this.createQuery(hql.toString(), list.toArray());
		Object result=query.uniqueResult();

		return (result == null ? null : (Long)result );
	}
	/**
	 * 方法描述: Object To Domain
	 * @author 胡煦
	 * Create on:2008-12-19
	 * @param List<Object[]>
	 * @return List<EmergencyDegreeDO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private List<EmergencyDegreeDO> changeObjToDo(List<Object[]> listObj){
		List<EmergencyDegreeDO> list = new ArrayList<EmergencyDegreeDO>();
		for(Object[] objects : listObj){
			EmergencyDegreeDO emergencyDegreeDO = new EmergencyDegreeDO();
			emergencyDegreeDO.setKeyword(objects[0]==null?null:new Long(objects[0].toString()));
			emergencyDegreeDO.setName(objects[1]==null?null:objects[1].toString());
			emergencyDegreeDO.setRemark(objects[2]==null?null:objects[2].toString());
			emergencyDegreeDO.setSequence(objects[3]==null?null:Short.parseShort(objects[3].toString()));
			list.add(emergencyDegreeDO);
		}
		return list;
	}
}
